package com.hjk.waibao.mj.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;

/**
 * @Description 基础数据转换类，用于将特定基础数据类型转化为别的基础数据类型
 * @Author hjk
 * @Date 2019/3/12
 **/
public class BaseDataConvertUtil {
    public static Set<Class> classSet = CollUtil.newHashSet(String.class, Double.class, Integer.class
            , BigDecimal.class, Long.class, Boolean.class, Date.class);

    public static boolean inConvertType(Object object) {
        return object == null ||
                classSet.contains(object.getClass());

    }

    public static Object convert(Object object, Class<?> c) {
        if (object == null)
            return null;
        else if (object.getClass().equals(c)) {
            return object;
        } else if (c == String.class) {
            if (object instanceof Date)
                return DateUtil.formatDateTime((Date) object);
            return object.toString();
        } else if (c == Double.class) {
            if (object instanceof Integer)
                return ((Integer) object).doubleValue();
            if (object instanceof Double)
                return object;
            if (object instanceof Long)
                return ((Long) object).doubleValue();
            if (object instanceof BigDecimal)
                return ((BigDecimal) object).doubleValue();
            if (object instanceof Boolean)
                return (Boolean) object ? 1.0D : 0.0D;
            if (object instanceof Date)
                return (double) ((Date) object).getTime();
            try {
                return Double.valueOf(object.toString());
            } catch (Exception e) {
                return null;
            }
        } else if (c == Integer.class) {
            if (object instanceof Integer)
                return object;
            if (object instanceof Double)
                return ((Double) object).intValue();
            if (object instanceof Long)
                return ((Long) object).intValue();
            if (object instanceof BigDecimal)
                return ((BigDecimal) object).intValue();
            if (object instanceof Boolean)
                return (Boolean) object ? 1 : 0;
            if (object instanceof Date)
                return (int) ((Date) object).getTime();
            try {
                return Integer.valueOf(object.toString());
            } catch (Exception e) {
                return null;
            }
        } else if (c == Long.class) {
            if (object instanceof Integer)
                return ((Integer) object).longValue();
            if (object instanceof Double)
                return ((Double) object).longValue();
            if (object instanceof Long)
                return object;
            if (object instanceof BigDecimal)
                return ((BigDecimal) object).longValue();
            if (object instanceof Boolean)
                return (Boolean) object ? 1L : 0L;
            if (object instanceof Date)
                return ((Date) object).getTime();
            try {
                return Long.valueOf(object.toString());
            } catch (Exception e) {
                return null;
            }
        } else if (c == Boolean.class) {
            if (object instanceof Integer)
                return !object.equals(0);
            if (object instanceof Double)
                return !object.equals(0D);
            if (object instanceof Long)
                return !object.equals(0L);
            if (object instanceof BigDecimal)
                return ((BigDecimal) object).intValue() != 1;
            if (object instanceof Boolean)
                return object;
            if (object instanceof Date)
                return object != null;
            try {
                return "true".equals(object.toString()) || "1".equals(object.toString());
            } catch (Exception e) {
                return null;
            }
        } else if (c == Date.class) {
            if (object instanceof Integer)
                return new Date((Integer) object);
            if (object instanceof Double)
                return new Date(((Double) object).longValue());
            if (object instanceof Long)
                return new Date((Long) object);
            if (object instanceof BigDecimal)
                return new Date(((BigDecimal) object).longValue());
            if (object instanceof Boolean)
                return (Boolean) object ? new Date() : null;
            if (object instanceof Date)
                return object;
            try {
                return DateUtil.parse(object.toString());
            } catch (Exception e) {
                return null;
            }
        }
        return object;
    }

    public static Integer convertToInteger(Object object) {
        return (Integer) convert(object, Integer.class);
    }

    public static Double convertToDouble(Object object) {
        return (Double) convert(object, Double.class);
    }

    public static String convertToString(Object object) {
        return (String) convert(object, String.class);
    }

    public static Long convertToLong(Object object) {
        return (Long) convert(object, Long.class);
    }

    public static Date convertToDate(Object object) {
        return (Date) convert(object, Date.class);
    }

    public static void main(String[] args) {
        Object object = convert(1.0, Integer.class);
        System.out.println(object);
        System.out.println(object.getClass());
    }
}
